package com.gexiaobao.pigeon.ui.fragment.mine.pigeon

import android.os.Bundle
import android.view.View
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.app.model.bean.PigeonFosterBean
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.databinding.FragmentBasicInfoBinding
import com.gexiaobao.pigeon.ui.dialog.RxDialogDefault
import com.gexiaobao.pigeon.viewmodel.MyPigeonViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 17:15
 * @Describe    : 寄养基本信息
 */
class FragmentFosterPigeonBasicInfo : BaseFragment<MyPigeonViewModel, FragmentBasicInfoBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
    }

    override fun initData() {
        super.initData()
        mViewModel.pigeonType.set("2")
        showUIData()
    }

    private fun showUIData() {
        mDatabind.banner.visibility = View.GONE
        mViewModel.pigeonFeatherColor.set(mPigeonData.featherColor)
        mViewModel.pigeonMasterName.set(mPigeonData.memberName)
        mViewModel.pigeonRingId.set(mPigeonData.ringId)
        mViewModel.pigeonRingSn.set(mPigeonData.ringSn)

        when (mPigeonData.sex) {
            1 -> {
                mViewModel.pigeonSex.set(getString(R.string.hint_unknown))
            }

            2 -> {
                mViewModel.pigeonSex.set(getString(R.string.hint_female))
            }

            3 -> {
                mViewModel.pigeonSex.set(getString(R.string.hint_male))
            }
        }
        when (mPigeonData.eyePattern) {
            1 -> {
                mViewModel.pigeonEyePattern.set(getString(R.string.hint_unknown))//"未知"
            }

            2 -> {
                mViewModel.pigeonEyePattern.set(getString(R.string.pigeon_yellow))//"黄"
            }

            3 -> {
                mViewModel.pigeonEyePattern.set(getString(R.string.pigeon_sha))//"砂"
            }

            4 -> {
                mViewModel.pigeonEyePattern.set(getString(R.string.pigeon_bull))//"牛"
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvBasicInfoModify, mDatabind.tvBasicInfoDelete) {
            when (it) {
                mDatabind.tvBasicInfoModify -> {
                    nav().navigateAction(R.id.action_detail_pigeon_to_add_pigeon, Bundle().apply {
                        putString("title", getString(R.string.Pigeon_modification))
                        putSerializable("pigeonData", mPigeonData)
                        putString("featherColor", mViewModel.pigeonFeatherColor.get())
                        putInt("type", 2)
                    })
                }

                mDatabind.tvBasicInfoDelete -> {
                    deletePigeon()
                }
            }
        }
    }

    private fun deletePigeon() {
        val dialog = RxDialogDefault(context)
        dialog.setTitle(getString(R.string.is_delete_this_pigeon))
        dialog.setSureListener {
            showLoading(getString(R.string.loading))
            mViewModel.deletePigeon(mPigeonId)
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.deletePigeon.observe(viewLifecycleOwner) {
            dismissLoading()
            if (it.isSuccess) {
                nav().navigateUp()
                eventViewModel.toRefreshList.value = true
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }
    }

    companion object {
        var mPigeonId: String = ""
        lateinit var mPigeonData: PigeonFosterBean.ListData
        fun newInstance(pigeonId: String, pigeonData: PigeonFosterBean.ListData): FragmentFosterPigeonBasicInfo {
            val fragment = FragmentFosterPigeonBasicInfo()
            mPigeonId = pigeonId
            mPigeonData = pigeonData
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }
    }
}